package com.huaboot.system.strategy.context;

import com.huaboot.system.enums.UploadModeEnum;
import com.huaboot.system.strategy.UploadStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 上传附件策略上下文工厂
 * description: UploadStrategyContextFactory
 * date: 2023/9/28 11:35
 * author: ff
 */
@Component
@Slf4j
public class UploadStrategyContextFactory {
    /**Spring会将实现注入到该map 里面， key:注入的name,默认类名小写， value：为该实例 **/
    @Resource
    private Map<String, UploadStrategy> interFaceMap;

    public UploadStrategy getUploadStrategyServiceByMode(String mode){

        String implementName  = UploadModeEnum.getEnumByMode(mode);

        UploadStrategy uploadStrategyBean = interFaceMap.get(implementName);

        if (uploadStrategyBean == null ){
            log.error("上传策略工厂bean异常:{} bean is null",implementName);
            throw new RuntimeException("bean is null");
        }

        return uploadStrategyBean;
    }


}
